package rxjava2.groccery;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.functions.Consumer;
import io.reactivex.subjects.PublishSubject;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * Created by SEELE on 2018/8/6.
 */
public class PublishSubjectBus {
    public static void main(String[] args) {
        rxbus();
//        subscribeSubject();
    }

    private static void subscribeSubject() {
        PublishSubject publishSubject = PublishSubject.create();

        publishSubject.subscribe(new Consumer() {
            public void accept(Object o) throws Exception {
                System.out.println(o);
            }
        });


        Observable.just("Single").subscribe(publishSubject);
        /*上面这段代码的逻辑就是先从observable发射数据给publishSubject，然后subject会调用onNext，把数据给subscriber
         * */
    }

    private static void rxbus() {
        Consumer<ScaryEvent> consumer = new Consumer<ScaryEvent>() {
            public void accept(ScaryEvent scaryEvent) throws Exception {
                System.out.println(scaryEvent.name);
            }
        };
        RxBus.getInstance().subscribe(ScaryEvent.class, consumer);

        RxBus.getInstance().subscribe(HappyEvent.class, new Consumer<HappyEvent>() {
            public void accept(HappyEvent happyEvent) throws Exception {
                System.out.println(happyEvent.name);
            }
        });

        Consumer<String> consumer1 = new Consumer<String>() {
            public void accept(String o) throws Exception {
                System.out.println(o);
            }
        };
        RxBus.getInstance().subscribe(String.class, consumer1);

        Type type = consumer.getClass().getGenericSuperclass();
        System.out.println(type);
        RxBus.getInstance().post(new HappyEvent("ChristmasCherry"));
        RxBus.getInstance().post(new ScaryEvent("911"));
    }


    public static final class RxBus {
        private static final RxBus INSTANCE;

        static {
            INSTANCE = new RxBus();
        }

        public static RxBus getInstance() {
            return INSTANCE;
        }

        PublishSubject publishSubject = PublishSubject.create();

        @SuppressWarnings("unchecked")
        public void post(Object event) {
            publishSubject.onNext(event);
        }

        @SuppressWarnings("unchecked")
        public <T> void subscribe(Class<T> clazz, Observer<T> observer) {
            publishSubject.ofType(clazz).subscribe(observer);
        }

        @SuppressWarnings("unchecked")
        public <T> void subscribe(Class<T> clazz, Consumer<T> consumer) {
            publishSubject.ofType(clazz).subscribe(consumer);
        }

    }


    public static final class ScaryEvent {
        String name;

        public ScaryEvent(String name) {
            this.name = name;
        }
    }

    public static final class HappyEvent {
        String name;

        public HappyEvent(String name) {
            this.name = name;
        }
    }
}
